<?php
session_start();

$conn=new mysqli("localhost","root","123456","htmlkeshe");
$conn->query("set names utf8");

$sql="show table status where `name`='news';";

$result=$conn->query($sql) or die(mysqli_error($conn));

if($result->num_rows>0) {
    while ($row = $result->fetch_assoc()) {
        $num = $row['Auto_increment'];//获取当前新闻的id
    }
}
//创建对应的文件夹
$url="./resource/news/news".$num;
mkdir($url,0777,true);

//保存图片值文件夹
$picurl=$url."/news".$num.".jpeg";
if(is_uploaded_file($_FILES['uppicture']['tmp_name'])){
    if(move_uploaded_file($_FILES['uppicture']['tmp_name'],$picurl)){
        //图片上传成功
    }
}
//所有的新闻数据
$text=$_POST['news'];
$txturl=$url."/".$_POST['title'].'.txt';
$myfile = fopen($txturl, "w");
fwrite($myfile, $text);
fclose($myfile);
$time =date("Y-m-d H:i:s");
//写入数据库
$sql="INSERT INTO news (title,newsTime,authorId,authorName,newsPic,newsContent,whichGame,heat) VALUES ('".$_POST['title']."','".$time."',".$_SESSION["id"].",'".$_SESSION["name"]."','".$picurl."','".$txturl."','".$_POST["select"]."',1);";
$result = $conn->query($sql) or die(mysqli_error($conn));

$sql="SELECT * from user where id=".$_SESSION["id"];
$result=$conn->query($sql) or die(mysqli_error($conn));;
$row = $result->fetch_assoc();
$num = $row["nrOfNews"]+1;
$sql = "UPDATE user set nrOfNews=$num WHERE id=".$_SESSION["id"];
$conn->query($sql);
$conn->close();
header('Refresh: 0; url=Personal.php');
//echo "<a href='Personal.php'>正在跳转到个人界面,若长时间未响应,则点击此处</a>";
?>
<script>
    alert("发布成功!")
</script>
